package dao.impl;

import dao.BaseDao;
import dao.OrderDao;
import domain.Order;
import domain.UserScore;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class OrderDaoImpl extends BaseDao implements OrderDao {

    @Override
    public int saveOrder(Order order) {
        Connection connection = null;
        int update = 0;
        try {
            connection = JDBCUtils.getDruidConnection();
            String sql = "insert into t_order(order_id,create_time,price,status,user_id) values(?,?,?,?,?);";
            update = update(connection, sql, order.getOrderId(), order.getCreateTime(), order.getPrice(), order.getStatus(), order.getUserId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection,null);
        }
        return update;
    }

    @Override
    public List<Order> queryOrders() {
        Connection connection = null;
        List<Order> list = null;
        try {
            connection = JDBCUtils.getDruidConnection();
            String sql = "select order_id orderId,create_time createTime,price,status,user_id userId from t_order;";
            list = queryForMore(connection,sql,Order.class);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection,null);
        }
        return list;

    }

    @Override
    public int changeOrderStatus(String orderId, Integer status) {
        Connection connection = null;
        int update = 0;
        try {
            connection = JDBCUtils.getDruidConnection();
            String sql = "update t_order set status = ? where order_id = ?;";
            update = update(connection, sql, status, orderId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection,null);
        }
        return update;
    }

    @Override
    public List<Order> queryOrdersByUserId(Integer userId) {
        Connection connection = null;
        List<Order> list = null;
        try {
            connection = JDBCUtils.getDruidConnection();
            String sql = "select order_id orderId,create_time createTime,price,status,user_id userId from t_order where user_id = ?;";
            list = queryForMore(connection,sql,Order.class,userId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection,null);
        }
        return list;
    }

    @Override
    public int insertUserScore(Integer uid, String score) {
        Connection connection = null;
        int update = 0;
        try {
            connection = JDBCUtils.getDruidConnection();
            String sql = "insert into userscore(uid,score) values(?,?);";
            update = update(connection, sql, uid,score);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection,null);
        }
        return update;
    }

    @Override
    public UserScore selectUserScope(Integer uid) {
        Connection connection = null;
        UserScore userScore = null;
        try {
            connection = JDBCUtils.getDruidConnection();
            String sql = "select uid,score from userscore where uid = ?;";
            userScore = queryForOne(connection, sql, UserScore.class, uid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection,null);
        }
        return userScore;
    }

    @Override
    public List<UserScore> selectAllUserScore() {
        Connection connection = null;
        List<UserScore> list = null;
        try {
            connection = JDBCUtils.getDruidConnection();
            String sql = "select uid,score from userscore;";
            list = queryForMore(connection,sql,UserScore.class);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection,null);
        }
        return list;
    }
}
